package com.colorfruit.admin.control.controller;

import com.alibaba.fastjson.JSONObject;
import com.colorfruit.admin.business.service.ProductService;
import com.colorfruit.admin.control.annotation.Auth4Admin;
import com.colorfruit.admin.pojo.entity.FoundationProduct;
import com.colorfruit.admin.pojo.entity.PrePurchaseOrder;
import com.colorfruit.admin.pojo.enums.AdminRedisEnum;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.admin.pojo.model.SysUserModel;
import com.colorfruit.admin.pojo.vo.prepurchase.PrePurchaseDetail;
import com.colorfruit.admin.pojo.vo.prepurchase.PrePurchaseOrderVo;
import com.colorfruit.admin.pojo.vo.product.FoundationProductVo;
import com.colorfruit.framework.model.pojo.BaseResult;
import com.colorfruit.framework.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    RedisCache redisCache;

    @Autowired
    HttpServletRequest request;

    /**
     * 插入商品
     * @param foundationProductVo
     * @return
     */
    @Auth4Admin
    @PostMapping("/insertProduct")
    public BaseResult insertProduct(@RequestBody FoundationProductVo foundationProductVo){
        try {
            log.info("# [IN] [controller] insertProductBasic param: foundationProductVo = {}", foundationProductVo.toString());
            BaseResult baseResult = productService.insertProduct(foundationProductVo);
            log.info("# [OUT] [controller] insertProductBasic SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询一二级商品列表
     * @param productName productName产品名称
     * @return
     */
    @Auth4Admin
    @GetMapping("/selectTwoLevelProductList")
    public BaseResult selectProductList(String productName){
        try {
            log.info("# [IN] [controller] insertProductBasic param: productName = {}", productName);
            String s = productService.selectTwoLevelProductList(productName);
            log.info("# [OUT] [controller] insertProductBasic SUCCESS");
            return new BaseResult(s);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询商品编辑信息
     * @param productId productName产品名称
     * @return
     */
    @Auth4Admin
    @GetMapping("/getProductEditInfo")
    public BaseResult getProductEditInfo(Long productId){
        try {
            log.info("# [IN] [controller] getProductEditInfo param: productId = {}", productId);
            BaseResult baseResult = productService.getProductEditInfo(productId);
            log.info("# [OUT] [controller] getProductEditInfo SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }


    /**
     * 编辑产品信息
     * @param foundationProduct 产品
     * @return
     */
    @Auth4Admin
    @PostMapping("/editProduct")
    public BaseResult editProduct(@RequestBody FoundationProduct foundationProduct){
        try {
            log.info("# [IN] [controller] insertProductBasic param: foundationProduct = {}", foundationProduct);
            BaseResult baseResult = productService.editProduct(foundationProduct);
            log.info("# [OUT] [controller] insertProductBasic SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 作废产品
     * @param productId
     * @return
     */
    @Auth4Admin
    @GetMapping("/deleteProduct")
    public BaseResult deleteProduct(Long productId){
        try {
            log.info("# [IN] [controller] deleteProductBasic param: productId = {}",productId);
            BaseResult baseResult = productService.deleteProduct(productId);
            log.info("# [OUT] [controller] deleteProductBasic SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 插入预采购订单
     * @param prePurchaseOrderVo
     * @return
     */
    @Auth4Admin
    @PostMapping("/insertPrePurchaseOrder")
    public BaseResult insertPrePurchaseOrder(@RequestBody PrePurchaseOrderVo prePurchaseOrderVo){
        try {
            log.info("# [IN] [controller] insertPrePurchaseOrder param: prePurchaseOrderVo = {}",prePurchaseOrderVo.toString());
            BaseResult baseResult = productService.insertPrePurchaseOrder(prePurchaseOrderVo);
            log.info("# [OUT] [controller] insertProduct SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询一二级预采购列表
     * @param productName   产品名称
     * @param recordStatus  状态 1. 待审核 2.后备 3. 审核通过 不传查全部
     * @return
     */
    @Auth4Admin
    @GetMapping("/selectTwoLevelPrePurchaseList")
    public BaseResult selectTwoLevelPrePurchaseList(String productName, Byte recordStatus ){
        try {
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller] selectTwoLevelPrePurchaseList param: userId = {}, productName = {}, recordStatus = {}",userId, productName, recordStatus);
            String s = productService.selectTwoLevelPrePurchaseList(userId, productName, recordStatus);
            log.info("# [OUT] [controller] selectTwoLevelPrePurchaseList SUCCESS");
            return new BaseResult(s);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询商品编辑信息
     * @param prePurchaseOrderId productName产品名称
     * @return
     */
    @Auth4Admin
    @GetMapping("/getPreEditProductInfo")
    public BaseResult getPreEditProductInfo(Long prePurchaseOrderId){
        try {
            log.info("# [IN] [controller] getProductEditInfo param: productId = {}", prePurchaseOrderId);
            PrePurchaseOrder preEditProductInfo = productService.getPreEditProductInfo(prePurchaseOrderId);
            log.info("# [OUT] [controller] getProductEditInfo SUCCESS");
            return new BaseResult(preEditProductInfo);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 编辑预采购订单
     * @param prePurchaseOrderVo
     * @return
     */
    @Auth4Admin
    @PostMapping("/editPrePurchaseOrder")
    public BaseResult editPrePurchaseOrder(@RequestBody PrePurchaseOrderVo prePurchaseOrderVo){
        try {
            log.info("# [IN] [controller] insertPrePurchaseOrder param: prePurchaseOrderVo = {}",prePurchaseOrderVo.toString());
            BaseResult baseResult = productService.editPrePurchaseOrder(prePurchaseOrderVo);
            log.info("# [OUT] [controller] insertProduct SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询商品预采购详情
     * @param productId
     * @return
     */
    @Auth4Admin
    @GetMapping("/getPrePurchaseDetail")
    public BaseResult getPrePurchaseDetail(Long productId){
        try {
            log.info("# [IN] [controller] deleteProduct param: productId = {}",productId);
            PrePurchaseDetail prePurchaseDetail = productService.getPrePurchaseDetail(productId);
            log.info("# [OUT] [controller] deleteProduct SUCCESS");
            return new BaseResult(prePurchaseDetail);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 审核接口
     * @param purchaseDetail
     * @return
     */
    @Auth4Admin
    @PostMapping("/generatePreOnShelfOrder")
    public BaseResult generatePreOnShelfOrder(@RequestBody PrePurchaseDetail purchaseDetail, HttpServletRequest request){
        try {
            log.info("# [IN] [controller] auditProduct param: foundationProductVo = {}",purchaseDetail.toString());
            String token = request.getHeader("token");
            String userModelString = redisCache.getCache(AdminRedisEnum.SYS_USER_PREFIX.getKey() + token, String.class);
            SysUserModel sysUserModel = JSONObject.parseObject(userModelString, SysUserModel.class);
            String auditName = sysUserModel.getUserName();
            BaseResult baseResult = productService.generatePreOnShelfOrder(purchaseDetail, auditName);
            log.info("# [OUT] [controller] auditProduct SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 查询预上架商品列表
     * @param productName
     * @return
     */
    @Auth4Admin
    @GetMapping("/selectPreShelfList")
    public BaseResult selectPreShelfList(String  productName, int page, int size){
        try {
            log.info("# [IN] [controller] selectPreShelfList param: productId = {}",productName);
            BaseResult baseResult = productService.selectPreOnShelfProductList(productName, page, size);
            log.info("# [OUT] [controller] selectPreShelfList SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 计算销售价格
     * @param prePurchaseOrderIds 预采购订单ID以逗号分隔
     * @return
     */
    @Auth4Admin
    @GetMapping("/countSalePrice")
    public BaseResult countSalePrice(String prePurchaseOrderIds){
        try {
            log.info("# [IN] [controller] countSalePrice param: prePurchaseOrderIds = {}",prePurchaseOrderIds);
            BaseResult baseResult = productService.countSalePrice(prePurchaseOrderIds);
            log.info("# [OUT] [controller] countSalePrice SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @GetMapping("/canBePurchased")
    public BaseResult canBePurchased(Long productId){
        log.info("# [IN] [controller] canBePurchased param: productId = {}",productId);
        BaseResult baseResult = productService.canBePurchased(productId);
        log.info("# [OUT] [controller] canBePurchased SUCCESS");
        return baseResult;
    }

}
